package com.situ.shoplook.admin.api;

import com.github.pagehelper.PageInfo;
import com.situ.shoplook.common.model.ProductSpu;
import com.situ.shoplook.common.model.search.ProductSpuSearchBean;
import com.situ.shoplook.common.service.BrandService;
import com.situ.shoplook.common.service.CategoryService;
import com.situ.shoplook.common.service.ProductSpuService;
import com.situ.shoplook.common.service.UploadFileService;
import com.situ.shoplook.common.util.JsonResult;
import com.situ.shoplook.common.util.PaginateParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * <p>
 * created by 千堆雪 on 2024/8/26, last modified by 千堆雪 on 2024/8/26
 */
@RestController
@RequestMapping(value = "/api/v1/products/spu", produces = "application/json;charset=utf-8")
public class ProductSpuApi {
    private ProductSpuService productSpuService;
    private UploadFileService uploadFileService;
    private CategoryService categoryService;
    private BrandService brandService;

    @Autowired
    public void setProductSpuService(ProductSpuService productSpuService) {
        this.productSpuService = productSpuService;
    }

    @Autowired
    public void setUploadFileService(UploadFileService uploadFileService) {
        this.uploadFileService = uploadFileService;
    }

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Autowired
    public void setBrandService(BrandService brandService) {
        this.brandService = brandService;
    }

    @GetMapping
    private JsonResult<Map<String, Object>> findAll(ProductSpuSearchBean psb,
                                                    @RequestParam(defaultValue = "1") Integer pageNo,
                                                    @RequestParam(defaultValue = "2147383647") Integer pageSize) {
        List<ProductSpu> list = productSpuService.findAll(psb, PaginateParams.of(pageNo, pageSize));

        for (ProductSpu spu : list) {
            if (spu.getCategoryId() != null) {
                spu.setCategory(this.categoryService.findById(spu.getCategoryId()));
            }
            if (spu.getBrandId() != null) {
                spu.setBrand(this.brandService.findById(spu.getBrandId()));
            }
        }

        PageInfo<ProductSpu> pi = new PageInfo<>(list);
        return JsonResult.success(Map.of("rows", list, "pi", Map.of("total", pi.getTotal(),
                "pages", pi.getPages(), "pageNo", pi.getPageNum(), "pageSize", pi.getPageSize())));
    }

    @GetMapping("/id")
    public JsonResult<?> findById(Integer id) {
        ProductSpu ps = this.productSpuService.findById(id);
        if (ps == null) {
            return JsonResult.fail("未找到指定编写的商品基础信息");
        }
        return JsonResult.success(ps);
    }


    @PostMapping
    public JsonResult<?> save(@RequestBody ProductSpu productSpu) {
        boolean success = this.productSpuService.save(productSpu);
        return JsonResult.of(success, null);
    }

    @PutMapping
    public JsonResult<?> update(@RequestBody ProductSpu productSpu) {
        boolean success = this.productSpuService.update(productSpu);
        return JsonResult.of(success, null);
    }

    @DeleteMapping
    public JsonResult<?> deleteByIds(@RequestBody Integer[] ids) {
        int rows = this.productSpuService.deleteByIds(ids);
        return JsonResult.of(rows > 0, Map.of("rows", rows, "msg", rows > 0 ? "操作成功，共删除" + rows + "行" : "操作失败"));
    }


    /**
     * 上传商品图片
     *
     * @return 头像
     */
    @PostMapping("/pic")
    public JsonResult<?> uploadPic(MultipartFile file) {
        //对于分布式部署，就应该将图片上传到阿里云，或七牛云，或其它图床
        String url = this.uploadFileService.uploadImage(file, "product");
        return JsonResult.success(url);
    }

    //富文本编辑器上传图片
    @PostMapping("/editor")
    public Map<String, Object> uploadEditor(MultipartFile file) {
        //对于分布式部署，就应该将图片上传到阿里云，或七牛云，或其它图床
        String url = this.uploadFileService.uploadImage(file, "product/editor");
        return Map.of("errno", 0, "data", Map.of("url", url));
    }


}
